﻿using System;
using System.Linq;
using System.Web;
using System.Data;
using Lib.Data;

namespace Lib.Utilities
{
    public class Until
    {

        /// <summary>
        /// Kiểm tra nếu chuỗi NULL thì trả về rỗng
        /// </summary>
        /// <param name="val">Chuỗi cần kieemrt ra</param>
        public static string NVL(string val)
        {
            if (string.IsNullOrEmpty(val))
                return "";
            return val.Trim();
        }
        /// <summary>
        /// Kiểm tra chuỗi, nếu rỗng hoạc Null thì trả lại OutVal
        /// </summary>
        /// <param name="val">Chuỗi cần kiểm tra</param>
        /// <param name="outVal">Chuỗi Trả về</param>
        public static string NVL(string val, string outVal)
        {
            if (string.IsNullOrEmpty(val))
                return outVal;
            return val.Trim();
        }


        /// <summary>
        /// Xóa  một phần từ có tên trong mảng
        /// </summary>
        /// <param name="arr">Mảng cần kiểm tra xóa</param>
        /// <param name="name">Tên kiểm tra</param>
        public static string[] RemoveElement(string[] arr, string name)
        {
            if (arr.Length <= 0) return arr;
            return arr.Where(p => p != name).ToArray();
        }
        public static DataRow FindUser()
        {
            DataRow row = null;
            DataTable table = (DataTable)HttpContext.Current.Application["connec"];
            row = table.Rows[0];
            return row;
        }
        public static DataRow FindUser(string userName)
        {
            DataRow row = null;
            DataTable table = (DataTable)HttpContext.Current.Application["connec"];
            int num = LibTable.FindRowByColumn(table, "user", userName);
            if (num >= 0)
            {
                row = table.Rows[num];
            }
            //lưu connecstring
            DataOra _ora = new DataOra();
            _ora.connecString = LibConvert.ObjectToString(row["connec"]);
            return row;
        }
        public static void Initialization()
        {
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(HttpContext.Current.Server.MapPath("~/htdns/config.xml"));
                DataTable table = (DataTable)HttpContext.Current.Application["connec"];
                table.AcceptChanges();
                DataTable table2 = set.Tables["info"];
                if (table2 != null)
                {
                    int count = table2.Rows.Count;
                    if (count != 0)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            DataRow row2 = table2.Rows[i];
                            bool flag = true;
                            for (int j = 0; j < i; j++)
                            {
                                DataRow row3 = table2.Rows[j];
                                if ((((row3["ip"].ToString() == row2["ip"].ToString()) && (row3["db"].ToString() == row2["db"].ToString())) && ((row3["loai"].ToString() == row2["loai"].ToString()) && (row3["dbo"].ToString() == row2["dbo"].ToString()))) && ((row3["ma_dvi"].ToString() == row2["ma_dvi"].ToString()) && (row3["nsd"].ToString() == row2["nsd"].ToString())))
                                {
                                    flag = false;
                                    break;
                                }
                            }
                            if (flag)
                            {
                                DataRow row = table.NewRow();
                                row["loai"] = row2["loai"];
                                row["dbo"] = row2["dbo"];
                                row["user"] = row2["user"];
                                row["nsd"] = row2["nsd"];
                                if (row2["loai"].ToString() == "ORA")
                                {
                                    row["connec"] = string.Concat(new object[] { "User ID=", row["nsd"], "; Password=", row2["pas"], ";" });
                                    row["connec"] = row["connec"] + "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)";
                                    row["connec"] = string.Concat(new object[] { row["connec"], "(HOST=", row2["ip"], ")(PORT=", row2["port"], "))" });
                                    row["connec"] = string.Concat(new object[] { row["connec"], "(CONNECT_DATA=(SID=", row2["db"], ")))" });
                                }
                                else if (LibConvert.ObjectToString (row2["loai"]) == "MSSQL")
                                {
                                    row["connec"] = string.Concat(new object[] { "User ID =", row["nsd"], "; Data Source=", row2["ip"], "; Initial Catalog=", row2["db"], ";" });
                                    if (LibConvert.ObjectToString(row2["pas"]) != "")
                                    {
                                        row["connec"] = string.Concat(new object[] { row["connec"], " Password=", row2["pas"], ";" });
                                    }
                                    row["dbo"] = "dbo";
                                }
                                table.Rows.Add(row);
                            }
                        }
                        table.AcceptChanges();
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("loi:Lỗi khởi tạo tham số kết nối " + e.Message + " :loi");
            }
        }
    }
}
